#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_errno.h"
#include "hi_io.h"
#define LED_PIN_NAME 6   // 6号引脚可以控制智能风扇LED
#define LED_PIN_FUNCTION HI_IO_FUNC_GPIO_6_PWM3_OUT
#define LED_PWM 3        // 6号引脚对应PWM3
#define RESOLUTION 4096
#define PWM_FREQ_DIVITION 64000
static void PWMLedDemoTask(void *arg)
{
 (void)arg;
 //交通灯板的红灯
 IoTGpioSetFunc(LED_PIN_NAME, LED_PIN_FUNCTION);
 //初始化一个 PWM 模块
 IoTPwmInit(LED_PWM);

 while (1)
 {
    // use PWM control RED LED brightness
    for (int i = 1; i <= RESOLUTION; i *= 2)
    {
        //PWM 开始
        IoTPwmStart(LED_PWM, i, PWM_FREQ_DIVITION);
        //间隔时间
        usleep(250000);
        //PWM 停止
        IoTPwmStop(LED_PWM);
    }
    // for (int i = RESOLUTION; i >= 1; i -= 2)
    // {
    //     //PWM 开始
    //     IoTPwmStart(i, 1, PWM_FREQ_DIVITION);
    //     //间隔时间
    //     usleep(250000);
    //     //PWM 停止
    //     IoTPwmStop(1);
    // }
 }
}
static void PWMLedDemo(void)
{
 osThreadAttr_t attr;
 // set Red/Green/Blue LED pin to GPIO function
 IoTGpioInit(LED_PIN_NAME); //电机模块的led

 attr.name = "PWMLedDemoTask";
 attr.attr_bits = 0U;
 attr.cb_mem = NULL;
 attr.cb_size = 0U;
 attr.stack_mem = NULL;
 attr.stack_size = 4096;
 attr.priority = osPriorityNormal;
 if (osThreadNew(PWMLedDemoTask, NULL, &attr) == NULL)
 {
 printf("[ColorfulLightDemo] Falied to create PWMLedDemoTask!\n");
 }
}
SYS_RUN(PWMLedDemo);